﻿using BingoClientModel;
using BingoServerCommon.Common;
using Dapper;
using MySql.Data.MySqlClient;


namespace BingoHallServerConsole.Business.Account
{
	/// <inheritdoc />
	public class CS_GetProfile : Package
	{


		public CS_GetProfile(EnumPackCommand mainid) : base(mainid)
		{
		}

		public CS_GetProfile(byte[] buffer, int len) : base(buffer, len)
		{
		}

		public override Package Clone(byte[] bytes)
		{
			return new CS_GetProfile(bytes, bytes.Length);
		}





		/// <summary>
		/// 得到用户的profile，用户资料页签
		/// </summary>
		public override void Excute(Session session)
		{
			var playerId = ReadInt();

			MySqlConnection conn = Program.ConnectionPool.getConnection(); //从连接池拿到一个连接
			string sql = $"SELECT p.bingo_times,p.cards_played,p.admission_ticket_used,p.first_login,p.puzzles_completed from players p WHERE p.id = {playerId};";

			var package = new Package(EnumPackCommand.SC_GetProfile_Ret);

			var info = conn.QueryFirstOrDefault<dynamic>(sql);

			if (info != null)
			{
				//0表示成功，其余是失败
				package.Write((byte)EnumReqRetStatus.Success);
				
				package.Write((int)info.bingo_times);
				package.Write((int)info.cards_played);
				package.Write((int)info.admission_ticket_used);
				package.Write((int)info.first_login);
				package.Write((int)info.puzzles_completed);
				
			}
			else
			{
				//非0是失败
				package.Write((byte)EnumReqRetStatus.Fail0);
			}

			session.Send(package);

			Program.ConnectionPool.returnConnection(conn);//连接用完回收
		}


	}
}
